import { _decorator, Component, Node, Graphics, Input, EventTouch, Vec2, Vec3, tween } from 'cc';
import { data } from '../data';
import { G } from '../G';
const { ccclass, property } = _decorator;

@ccclass('hallScene')
export class hallScene extends Component {

    @property(Node)
    public graphics: Node;

    @property(Node)
    public mode: Node;

    @property(Node)
    public player: Node;

    @property(Node)
    public startBtn: Node;

    // 滑动相关
    public touchStartVec: Vec2 = new Vec2();
    public touchEndVec: Vec2 = new Vec2();

    // 模式
    public modeArray: Array<Node> = new Array<Node>(3);
    // 玩家
    public playerArray: Array<Node> = new Array<Node>(4);

    // 模式选择
    public modeSelected: number = 1;
    // 玩家选择
    public playerSelected: number = 1;

    start() {
        // 获取模式到数组
        this.modeArray[0] = this.mode.getChildByName('mode2')
        this.modeArray[1] = this.mode.getChildByName('mode1')
        this.modeArray[2] = this.mode.getChildByName('mode3')
        // 获取玩家到数组
        this.playerArray[0] = this.player.getChildByName('bird3')
        this.playerArray[1] = this.player.getChildByName('bird1')
        this.playerArray[2] = this.player.getChildByName('bird2')
        this.playerArray[3] = this.player.getChildByName('bird4')
        // 注册监听
        this.reg();
    }

    /**
     * 注册监听事件
     */
    reg() {
        // 滑动监听（选择模式）
        this.mode.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.mode.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
        // 滑动监听（选择玩家）
        this.player.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.player.on(Input.EventType.TOUCH_END, this.onTouchEnd2, this)
        // 开始游戏
        this.startBtn.on(Input.EventType.TOUCH_START, this.startGame, this)
    }

    /**
     * 游戏开始
     */
    startGame() {
        data.playerSelected = this.playerSelected
        data.gameMode = this.modeSelected
        G.enterGetReady()
    }

    /**
     * 开始触摸事件
     * @param et 
     * @returns 
     */
    onTouchStart(et: EventTouch): Function {
        this.touchStartVec = et.touch.getLocation()
        return
    }

    /**
     * 结束触摸事件(模式)
     * @param et 
     * @returns 
     */
    onTouchEnd(et: EventTouch): Function {
        this.touchEndVec = et.touch.getLocation()
        // 选择模式
        this.selectMode()
        return
    }

    /**
     * 结束触摸事件(玩家)
     * @param et 
     * @returns 
     */
    onTouchEnd2(et: EventTouch): Function {
        this.touchEndVec = et.touch.getLocation()
        // 选择玩家
        this.selectPlayer()
        return
    }

    // 选择模式
    selectMode() {
        let modePreSelected = this.modeSelected
        // 计算滑动方向
        if (this.touchEndVec.y - this.touchStartVec.y < 0) {
            // 向下滑
            this.modeSelected = this.modeSelected - 1 > 0 ? this.modeSelected - 1 : 0
        } else {
            // 向上滑
            this.modeSelected = this.modeSelected + 1 < 2 ? this.modeSelected + 1 : 2
        }
        this.selectModeAnimation(modePreSelected)
    }

    // 选择模式动画
    selectModeAnimation(modePreSelected: number) {
        if (modePreSelected != this.modeSelected) {
            for (let i = 0; i < this.modeArray.length; i++) {

                let pos = new Vec3()
                let scale = new Vec3()

                if (i == this.modeSelected - 1) {
                    pos.set(120, 50)
                    scale.set(0.6, 0.6)
                } else if (i == this.modeSelected + 1) {
                    pos.set(120, -50)
                    scale.set(0.6, 0.6)
                } else if (i == this.modeSelected) {
                    pos.set(120, 0)
                    scale.set(1, 1)
                } else if (i < this.modeSelected - 1) {
                    pos.set(120, 90)
                    scale.set(0, 0)
                } else if (i > this.modeSelected + 1) {
                    pos.set(120, -90)
                    scale.set(0, 0)
                }

                tween(this.modeArray[i])
                    .to(0.2, { scale: scale, position: pos }, {
                        easing: "circOut"
                    })
                    .start()
            }
        }
    }

    // 选择玩家
    selectPlayer() {
        let playerPreSelected = this.playerSelected
        // 计算滑动方向
        if (this.touchEndVec.x - this.touchStartVec.x > 0) {
            // 向右滑
            this.playerSelected = this.playerSelected - 1 > 0 ? this.playerSelected - 1 : 0
        } else {
            // 向左滑
            this.playerSelected = this.playerSelected + 1 < 3 ? this.playerSelected + 1 : 3
        }
        this.selectPlayerAnimation(playerPreSelected)
    }

    // 选择玩家动画
    selectPlayerAnimation(playerPreSelected: number) {
        if (playerPreSelected != this.playerSelected) {
            for (let i = 0; i < this.playerArray.length; i++) {

                let pos = new Vec3()
                let scale = new Vec3()

                if (i == this.playerSelected - 1) {
                    pos.set(-280, -15)
                    scale.set(0.6, 0.6)
                } else if (i >= this.playerSelected + 1) {
                    pos.set(-50, -10)
                    scale.set(0, 0)
                } else if (i == this.playerSelected) {
                    pos.set(-170, 0)
                    scale.set(1, 1)
                } else if (i < this.playerSelected - 1) {
                    pos.set(-330, -20)
                    scale.set(0, 0)
                }

                tween(this.playerArray[i])
                    .to(0.2, { scale: scale, position: pos }, {
                        easing: "circOut"
                    })
                    .start()
            }
        }
    }

}


